package com.learn.threadpool.service.impl;

import com.learn.threadpool.domain.Order;
import com.learn.threadpool.mapper.OrderMapper;
import com.learn.threadpool.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    @Override
    public void executeAsync(List<Order> results, OrderMapper orderMapper, CountDownLatch countDownLatch) {
        try {
            log.warn("start executeAsync");
            // 异步线程要做的事情
            orderMapper.saveBatchOrder(results);
            log.warn("end executeAsync");
        }finally {
            // 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放
            countDownLatch.countDown();
        }
    }
}
